package com.qianli.user.domain.facatory;

import com.qianli.user.domain.model.UserFrozenModel;
import com.qianli.user.domain.model.frozen.UserFrozen;
import com.qianli.user.domain.respository.UserFrozenRespository;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * name:冷冻信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/2 15:47
 * version: v1.0.0
 **/
@Service
public class UserFrozenFactory {
    @Autowired
    private UserFrozenRespository userFrozenRespository;
    /**
     * 加载冷冻信息
     * @param model
     */
    public void load(UserFrozenModel model) {
        userFrozenRespository.load(model);
        //过滤失效的冷冻信息
        List<UserFrozen> frozens = model.getFrozens();
        if(!CollectionUtils.isEmpty(frozens)){
            for(int i=0;i<frozens.size();i++){
                if(frozens.get(i).getFrozenEndDate().before(new Date())){
                    frozens.remove(i);
                    i--;
                }
            }
        }
    }

    /**
     * 存储冷冻信息
     * @param newUm
     */
    public void store(UserFrozenModel newUm) {
        userFrozenRespository.store(newUm);
    }
}
